package com.taobao.tddl.monitor.sql;

import java.sql.Statement;
import com.taobao.tddl.common.utils.logger.Logger;
import com.taobao.tddl.common.utils.logger.LoggerFactory;

import com.taobao.tddl.common.jdbc.Parameters;
import com.taobao.tddl.common.utils.extension.Activate;

@Activate(order = 0)
public class SqlLogMonitor implements SqlMonitor {

    private static final Logger logger = LoggerFactory.getLogger("sqlMonitor");

    public String matrixSql(String sql, Parameters params, String appName) {
        if (logger.isDebugEnabled()) {
            String text = "[Matrix] [" + appName + "] " + sql;
            if (params != null) {
                if (params.isBatch()) {
                    text = text + " [params] " + params.getBatchParameters().toString();
                } else if ((params.getCurrentParameter() != null) && (params.getCurrentParameter().size() > 0)) {
                    text = text + " [params] " + params.getCurrentParameter().toString();
                }
            }
            logger.debug(text);
        }
        return sql;
    }

    public String groupSql(String sql, Parameters params, String appName, String groupKey) {
        if (logger.isDebugEnabled()) {
            String text = "[Group ] [" + appName + "] [" + groupKey + "] " + sql;
            if (params != null) {
                if (params.isBatch()) {
                    text = text + " [params] " + params.getBatchParameters().toString();
                } else if ((params.getCurrentParameter() != null) && (params.getCurrentParameter().size() > 0)) {
                    text = text + " [params] " + params.getCurrentParameter().toString();
                }
            }
            logger.debug(text);
        }
        return sql;
    }

    public void atomSql(String sql, Statement stmt, String appName, String dbKey, String ip, String port) {
        if (logger.isDebugEnabled()) {
            logger.debug("[ Atom ] [" + appName + "] [" + dbKey + "(" + ip + ":" + port + ")] " + sql);
        }
    }

}
